package cac.gkinterface.rest;

import cac.common.CallInterFace.rest.SendMaterialInterface;
import cac.fenjian.resource.finProReceiveSearch.service.FinProReceiveSearchService;
import cac.fenjian.resource.mssslshelves.dao.WcsLogRecordDao;
import cac.fenjian.resource.mssslshelves.util.WcsLogRecord;
import cac.fenjian.resource.partreceivesearch.service.PartReceiveService;
import cac.fenjian.resource.wmsdeliverplan.dao.WmsDeliverPlanDao;
import cac.gkinterface.dao.ReCallWzDao;
import cac.gkinterface.dao.WorkPackageDao;
import cac.gkinterface.service.ReCallWzService;
import cac.gkinterface.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("ScheduledTask")
@EnableScheduling
public class ScheduledTask {
    @Autowired
    private Environment environment;
    @Autowired
    private WorkPackageDao workPackageDao;
    @Autowired
    FinProReceiveSearchService finProReceiveSearchService;
    @Autowired
    WcsLogRecordDao wcsLogRecordDao;
    @Autowired
    WcsLogRecord wcsLogRecord;
    @Autowired
    private ReCallWzDao reCallWzDao;
    @Autowired
    private SendMaterialInterface sendMaterialInterface;
    //xiaojian 5-15 更新台账为零的缓存箱数据
    @GetMapping("updateZeroWorkbin")
    @Transactional
    @Scheduled(cron = "0 0 3 * * ?")
    public HashMap updateZeroWorkbin() {

        HashMap hashMap = new HashMap();
        //1:备份要清理的数据

        String cleanHcxZero = environment.getProperty("pathProperty.cleanHcxZero");
        if ("false".equals(cleanHcxZero)) {
            return hashMap ;
        }
        //1：备份表要更新的数据
        workPackageDao.insertZeroWorkbin();//备份表要更新的数据
        //2：更新表数据
        workPackageDao.updateZeroWorkbin();
        //3：备份要删除的数据
        workPackageDao.insertDeleteWorkbin();//备份表要删除的数据
        //4：删除备份的数据
        workPackageDao.delZeroWorkbin();
        hashMap.put("retCode","成功");
        return hashMap;
    }

    /***
     * 定时任务清理wcs完成出库任务，wms任务没完成的数据，主要是针对wcs没有回调情况的异常数据处理
     * */
    @GetMapping("sysDealSslShelves")
    @Transactional
    @Scheduled(cron = "0 1 * * * ?")
    public String sysDealSslShelves(){
        long systime = System.currentTimeMillis();
        log.info("开始执行定时任务=========="+systime);
        //查询wms当前未完成的任务
        List<HashMap> tasks = wcsLogRecordDao.queryUnFinishTask();
        for(HashMap task : tasks){
            wcsLogRecord.sysUnFinishTask((String)task.get("palletCode"),systime);
        }
        log.info("执行定时任务结束=========="+systime);
        return "定时任务执行完毕";
    }
    //定时任务清理货架
    @GetMapping("cleanShevelsWorkbin")
    @Transactional
    @Scheduled(cron = "0 0 3 * * ?")
    public String cleanShevelsWorkbin(){
        long systime = System.currentTimeMillis();
        log.info("执行定时清理货架任务开始=========="+systime);
        wcsLogRecordDao.cleanShevelsWorkbin();
        log.info("执行定时清理货架任务结束=========="+systime);
        return "定时清理货架执行完成";
    }


    //定时任务清理货架
    @GetMapping("cleanLog")
    @Scheduled(cron = "0 0 4 * * ?")
    public String cleanLog(){
        long systime = System.currentTimeMillis();
        log.info("执行定时清理日志任务开始=========="+systime);
        reCallWzDao.deleteERPExchangeLog();
        reCallWzDao.deleteIssuedLog();
        log.info("执行定时清理日志任务结束=========="+systime);
        return "ok";
    }
    //定时查询mes是否移交
    @GetMapping("confirmDelivery")
    @Scheduled(cron = "0 0 4 * * ?")
    public void confirmDelivery() {
        log.info("定时查询未下账的数据");
        //网闸数据打包接口所需参数，以及发送文件所需func
        HashMap paramMap = new HashMap();
        //网闸查询场景编码
        paramMap.put("dmsCode", "ms_yjqr");
        //网闸查询数据SQL所需条件
        paramMap.put("source_type", "2");
        HashMap interfaceMap = new HashMap();
        interfaceMap.put("code", "getMatchData");
        interfaceMap.put("name", "定时上传数据配套场景的配套数据");
        interfaceMap.put("parameters", paramMap);
        //发送文件所需func
        interfaceMap.put("func", "101-MES-ConfirmDeliver");
        interfaceMap.put("sendScenario", "定时确认未移交数据");
        log.info("==========定时确认未移交数据===========" + interfaceMap.toString());
        //文件上传ERP
        HashMap rsl = sendMaterialInterface.SendFileInterface(interfaceMap);
        log.info("==========定时确认未移交数据===========" + rsl.toString());

    }
}
